//
//  HYBDPanFileListViewModel.m
//  XPlayer
//
//  Created by 张水 on 2023/9/15.
//

#import "HYBDPanFileListViewModel.h"
#import <HYVideoPlayer/HYVideoPlayerConfig.h>
#import "HYVideoViewmodel.h"
@implementation HYBDPanFileListViewModel

- (instancetype)init
{
    self = [super init];
    if(self){
        self.CellClickSubject = [RACSubject subject];
        self.CollectionCellClickSubject = [RACSubject subject];
        self.videoPlaySubject = [RACSubject subject];
    }
    return self;
}

- (void)loadPathDataWith:(NSString *)path listData:(GetListData)listData
{
    @weakify(self);
    [[BDPanOauthManager manager] subList:path bDFileInfo:^(id object) {
        @strongify(self);
        HYBDPanFileModel *model = [HYBDPanFileModel modelWithJson:object];
        [self listWithFileTypeWith:model.list];
        if (listData){
            listData();
        }
    }];
}
- (void)listWithFileTypeWith:(NSArray *)list
{
    NSMutableArray *tmpfileList = [NSMutableArray array];
    NSMutableArray *tmpaudioList = [NSMutableArray array];
    NSMutableArray *tmpvideoList = [NSMutableArray array];
    NSMutableArray *tmpotherList = [NSMutableArray array];
    NSMutableArray *tmppicList = [NSMutableArray array];
    NSMutableArray *tmpcloudALLFileList = [NSMutableArray array];
    for (int i = 0; i < list.count; i++) {
        HYBDPanFileModelList *model = list[i];
        if (model.category == BDPanFileTypeOther | model.category == BDPanFileTypeIAPP | model.category == BDPanFileTypeSeed) {
            [tmpotherList addObject:model];
        }
        if (model.category == BDPanFileTypeAudio) {
            [tmpaudioList addObject:model];
        }
        if (model.category == BDPanFileTypeVideo) {
            [tmpvideoList addObject:model];
        }
        if (model.category == BDPanFileTypePhoto) {
            [tmppicList addObject:model];
        }
        if (model.category == BDPanFileTypeDocument) {
            [tmpfileList addObject:model];
        }
        [tmpcloudALLFileList addObject:model];
    }
    self.otherList = [tmpotherList copy];
    self.audioList = [tmpaudioList copy];
    self.videoList = [tmpvideoList copy];
    self.picList = [tmppicList copy];
    self.fileList = [tmpfileList copy];
    self.cloudALLFileList = [tmpcloudALLFileList copy];
}
- (NSArray *)filetypeWith:(BDPanFileType)FileType
{
    if (FileType == BDPanFileTypeALL) {
        return self.cloudALLFileList;
    }else if (FileType == BDPanFileTypeOther){
        return self.otherList;
    }else if (FileType == BDPanFileTypeVideo){
        return self.videoList;
    }else if (FileType == BDPanFileTypeAudio){
        return self.audioList;
    }else if (FileType == BDPanFileTypeDocument){
        return self.fileList;
    }else if (FileType == BDPanFileTypePhoto){
        return self.picList;
    }else{
        return nil;
    }
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section withFileType:(BDPanFileType)FileType
{
    NSArray *array = [self filetypeWith:FileType];
    return array.count;
}

- (id)cellForRowAtIndexPath:(NSIndexPath *)indexPath withFileType:(BDPanFileType)FileType
{
    NSArray *array = [self filetypeWith:FileType];
    HYBDPanFileModelList *fileMeta = [array hy_objectAtIndex:indexPath.row];
    return fileMeta;
}

- (void)didSelectRowAtIndexPath:(NSIndexPath *)indexPath withFileType:(BDPanFileType)FileType
{
    NSArray *array = [self filetypeWith:FileType];
    HYBDPanFileModelList *fileMeta = [array hy_objectAtIndex:indexPath.row];
    if ([fileMeta.isdir isEqualToString:@"1"]) {
        [self.CellClickSubject sendNext:RACTuplePack(fileMeta,indexPath)];
        return;
    }
    if ((fileMeta.category == BDPanFileTypeIAPP) || (fileMeta.category == BDPanFileTypeOther) || (fileMeta.category == BDPanFileTypeSeed)) {
        return;
    }
    @weakify(self);
    [[BDPanOauthManager manager] searchFids:fileMeta.fs_id bDFileInfo:^(id responseObject) {
        @strongify(self);
        HYBDPanFileModel *model = [HYBDPanFileModel modelWithJson:responseObject];
        HYBDPanFileModelList *dlinkModel = [model.list hy_firstObject];
        [self.CellClickSubject sendNext:RACTuplePack(dlinkModel,indexPath)];
    }];
}

- (NSInteger)numberOfSectionsInCollectionView:(NSInteger)section withFileType:(BDPanFileType)FileType
{
    NSArray *array = [self filetypeWith:FileType];
    return array.count;
}

- (id)cellForRowAtIndexPathInCollectionView:(NSIndexPath *)indexPath withFileType:(BDPanFileType)FileType
{
    NSArray *array = [self filetypeWith:FileType];
    HYBDPanFileModelList *fileMeta = [array hy_objectAtIndex:indexPath.row];
    return fileMeta;
}

- (void)didSelectItemAtIndexPath:(NSIndexPath *)indexPath withFileType:(BDPanFileType)FileType
{
    NSArray *array = [self filetypeWith:FileType];
    HYBDPanFileModelList *fileMeta = [array hy_objectAtIndex:indexPath.row];
    if ([fileMeta.isdir isEqualToString:@"1"]) {
        [self.CollectionCellClickSubject sendNext:RACTuplePack(fileMeta,indexPath)];
        return;
    }
    if ((fileMeta.category == BDPanFileTypeIAPP) || (fileMeta.category == BDPanFileTypeOther) || (fileMeta.category == BDPanFileTypeSeed)) {
        return;
    }
    @weakify(self);
    //这里请求下是因为要获取到dlink 获取列表的时候没有给dlink字段
    [[BDPanOauthManager manager] searchFids:fileMeta.fs_id bDFileInfo:^(id responseObject) {
        @strongify(self);
        HYBDPanFileModel *model = [HYBDPanFileModel modelWithJson:responseObject];
        HYBDPanFileModelList *dlinkModel = [model.list hy_firstObject];
        [self.CollectionCellClickSubject sendNext:RACTuplePack(dlinkModel,indexPath)];
    }];
}

- (void)fetchVideoPlayUrlWithIndexPath:(NSIndexPath *)indexPath withFileType:(BDPanFileType)FileType
{
    NSArray *array = [self filetypeWith:FileType];
    HYBDPanFileModelList *fileMeta = [array hy_objectAtIndex:indexPath.row];
    [HYHub loadMessage:NSLocalizedString(@"加载中",nil)];
    @weakify(self);
    [[BDPanOauthManager manager] fetchVideoInfoWithModel:fileMeta completion:^(NSString *videoUrl) {
        @strongify(self);
        [HYHub dismiss];
        HYVideoModel *playModel = [[HYVideoModel alloc]init];
        NSString *videoName = [CommUtls fetchVideoNameWithFullName:fileMeta.server_filename];
        playModel.videoTitle = fileMeta.server_filename;
        playModel.videoUrl = videoUrl;
        playModel.videoImage = fileMeta.videoImage?:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:fileMeta.thumbs.url3]]];
        playModel.videoImagePath = [videoName stringByAppendingFormat:@".png"];
        HYVideoPlayerConfig *config = [[HYVideoPlayerConfig alloc]init];
        config.isBasePlayer = YES;
        config.currentVideoModel = playModel;
        config.videoSoucres = @[playModel];
        HYVideoViewModel *videoViewModel = [[HYVideoViewModel alloc]initWithFolderIdentification:@"baidu-vplayer" isPrivate:NO videoConfig:config];
        [self.videoPlaySubject sendNext:videoViewModel];
    }];
}

@end
